# Maintainer: Sergey Popov <popov.sa@gmail.com>

_realname=mlpack
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.4.2
pkgrel=1
pkgdesc='An intuitive, fast, scalable C++ machine learning library (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://mlpack.org/'
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-armadillo"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-ensmallen"
         "${MINGW_PACKAGE_PREFIX}-stb")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cotire"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
source=("https://www.mlpack.org/files/${_realname}-${pkgver}.tar.gz"
        "0001-mingw-w64-cmake.patch")
sha256sums=('9e5c4af5c276c86a0dcc553289f6fe7b1b340d61c1e59844b53da0debedbb171'
            'ee4c4e01799e831ac3447dd97eb1c947d14dcb1d6cf9bdee0181df5cf6e69491')
_src="${_realname}-${pkgver}"

prepare() {
  cd "${srcdir}/${_src}"
  patch -Np1 -i "${srcdir}/0001-mingw-w64-cmake.patch"
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  if check_option "debug" "y"; then
    _build_type="Debug"
  else
    _build_type="Release"
  fi

  LDFLAGS+=" -Wl,--export-all-symbols"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=${_build_type} \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTS=OFF \
    -DBUILD_PYTHON_BINDINGS=OFF \
    ../${_src}

  cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} cmake --install .

  install -Dm644 "${srcdir}/${_src}/COPYRIGHT.txt" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT.txt"
  install -Dm644 "${srcdir}/${_src}/LICENSE.txt" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt"
}
